package com.tongtong.stsuserapi.controller;

import com.tongtong.stscommon.message.STSMessage;
import com.tongtong.stscommon.proxy.annotation.AutoGetId;
import com.tongtong.stsuserapi.service.base.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单控制器
 */
@Api(tags="订单接口")
@RestController
@RequestMapping("/sts/user/order")
public class OrderController {

	@Autowired
	OrderService orderService;

	@ApiOperation("创建订单")
	@ApiImplicitParam(name = "userid", value = "该参数从token中自动获取，无需传入")
	@PostMapping
	@AutoGetId
	public STSMessage createOrder(Long userid, Long itemId, Integer quantity) {
		return orderService.createOrder(userid,itemId,quantity);
	}

	@ApiOperation("购物车批量创建订单")
	@ApiImplicitParam(name = "userid", value = "该参数从token中自动获取，无需传入")
	@PostMapping("/shoppingcart")
	@AutoGetId
	public STSMessage createItemsFromShoppingcart(Long userid, @RequestBody List<Long> shoppingcartIds) {
		return orderService.createOrdersFromShoppingcart(userid,shoppingcartIds);
	}

	@ApiOperation("取消订单")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "userid", value = "该参数从token中自动获取，无需传入"),
		@ApiImplicitParam(name = "reason",value = "取消原因")
	})
	@PatchMapping
	@AutoGetId
	public STSMessage cancelOrder(Long userid, Long orderId, String reason) {
		return orderService.cancelOrder(userid,orderId,reason);
	}

	@ApiOperation("完成订单")
	@ApiImplicitParam(name = "userid", value = "该参数从token中自动获取，无需传入")
	@PutMapping
	@AutoGetId
	public STSMessage completeOrder(Long userid, Long orderId) {
		return orderService.completeOrder(userid,orderId);
	}

	@ApiOperation("删除订单")
	@ApiImplicitParam(name = "userid", value = "该参数从token中自动获取，无需传入")
	@DeleteMapping
	@AutoGetId
	STSMessage deleteOrder(Long userid, Long orderId) {
		return orderService.deleteOrder(userid,orderId);
	}

	@ApiOperation("分类获取订单列表")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "userid", value = "该参数从token中自动获取，无需传入"),
		@ApiImplicitParam(name = "orderState",value = "订单状态(1为预定,2为完成,3为取消)")
	})
	@GetMapping("/list")
	@AutoGetId
	STSMessage getOrderList(Long userid, Integer orderState, Integer page, Integer pageSize) {
		return orderService.getOrderList(userid,orderState,page,pageSize);
	}

	@ApiOperation("获取订单详情")
	@ApiImplicitParam(name = "userid", value = "该参数从token中自动获取，无需传入")
	@GetMapping("/info")
	@AutoGetId
	STSMessage getOrderInfo(Long userid, Long orderId) {
		return orderService.getOrderInfo(userid,orderId);

	}

	@ApiOperation(value="分类关键词查询订单",notes="该接口无需传入id参数，用户id直接从token中获取")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userid", value = "该参数从token中自动获取，无需传入"),
			@ApiImplicitParam(name = "orderState",value = "订单状态(1为预定,2为完成,3为取消)"),
			@ApiImplicitParam(name = "keyword",value = "关键词(匹配商品名)")

	})
	@GetMapping("/search")
	@AutoGetId
	STSMessage searchOrder(Long userid, String keyword, Integer orderState, Integer page, Integer pageSize) {
		return orderService.searchOrder(userid,keyword,orderState,page,pageSize);
	}

}
